package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.po.MarketStorage;
import com.cskaoyan.bean.po.MarketStorageExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.chaosbean.storage.vo.StorageListVO;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.cskaoyan.service.adminservice.StorageService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author jackX
 * @datetime 2022/12/29 9:49
 * @description: TODO:
 */
@Component
public class StorageServiceImpl implements StorageService {
    @Autowired
    MarketStorageMapper storageMapper;

    @Override
    @Transactional
    public MarketStorage insertCreateFile(MultipartFile file) {
        //上传文件原始名字 //腾讯会议图片.png
        String originalFilename = file.getOriginalFilename();
        //key  替换掉原始ID.之前的部分
        String uuid = UUID.randomUUID().toString();
        String[] split = originalFilename.split("\\.");
        String key = uuid + "-" + split[0] + "." + split[1];
        //存储到本地
        File file1 = new File("C:\\ProjectTwo\\tmp\\", key);
        if (!file1.getParentFile().exists()) {
            file1.getParentFile().mkdirs();
        }
        try {
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //上传文件的type类型 //image/png
        String contentType = file.getContentType();
        //上传文件的大小 //115829
        long size3 = file.getSize();
        int size2 = (int) size3;
        Integer size = size2;
        //url
        String url = "http://localhost:8083/pic/" + key;
        MarketStorage marketStorage1 = new MarketStorage(null, key, originalFilename, contentType, size, url, new Date(), new Date(), true);
        storageMapper.insertMarketStorage(marketStorage1);
//        MarketStorage marketStorage = storageMapper.selectByPrimaryKey(marketStorage1.getId());
        return marketStorage1;
    }

    @Override
    public StorageListVO list(BasePageInfo info, String key, String name) {
        //开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        StorageListVO listVO = new StorageListVO();
        MarketStorageExample example = new MarketStorageExample();
        //排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketStorageExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(key)) {
            criteria.andKeyEqualTo(key);
        }
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameEqualTo(name);
        }
        List<MarketStorage> list = storageMapper.selectByExample(example);
        listVO.setList(list);
        PageInfo<MarketStorage> pageInfo = new PageInfo<>(list);
        listVO.setLimit(pageInfo.getPageSize());//limit
        listVO.setPage(pageInfo.getPageNum());//page
        listVO.setPages(pageInfo.getPages());//pages
        listVO.setTotal((int) pageInfo.getTotal());//total
        return listVO;
    }

    @Override
    public MarketStorage update(MarketStorage storage) {
        storageMapper.updateName(storage);
        return storage;
    }
    @Override
    public void delete(MarketStorage storage) {
        storageMapper.deleteByPrimaryKey(storage.getId());
    }
}
